package com.tongtailian.www.stir.activity;

import android.androidlib.utils.MathExtendUtils;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import com.tongtailian.www.stir.R;
import com.tongtailian.www.stir.base.BaseActivity;
import com.tongtailian.www.stir.bean.CleanConfigBean;
import com.tongtailian.www.stir.bean.ConfigBean;
import com.tongtailian.www.stir.bean.ConfigDataFram;
import com.tongtailian.www.stir.bleinterface.MyBleGattCallback;
import com.tongtailian.www.stir.utils.BleManageUtil;
import com.tongtailian.www.stir.utils.LogUtil;
import com.tongtailian.www.stir.utils.ProgressDlgUtil;
import com.tongtailian.www.stir.utils.ToastUtils;
import com.tongtailian.www.stir.utils.gson.GsonUtil;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 * 编辑查看配置.
 */
public class EditAndLookConfigActivity extends BaseActivity {



    private ConfigBean configBean;
    private ConfigDataFram configDataFram;
    private ConfigDataFram sendDataFram;
    //    private RecyclerView rvEdit;
//    private ConfigEditAdapter adapter;
    private List<CleanConfigBean> list = new ArrayList<>();
    private BleManageUtil bleManageUtil;
    private int position;
    private EditText etInput1;
    private EditText etInput2;
    private EditText etInput3;
    private EditText etInput4;
    private EditText etInput6;
    private EditText et2Input6;
    private EditText etInput7;
    private EditText et2Input7;
    private CheckBox chCheck5;
    private CheckBox chCheck8;
    private CheckBox chCheck9;
    private List<ConfigBean> runTypelist = new ArrayList<>();//选择配置
    private Button btWrite;

    private LinearLayout llLook;
    private TextView tvLookDetial;

    private boolean isEdittext = false;
    private Handler handler = new Handler();


    @Override
    public int setLayoutId() {
        return R.layout.activity_confit_edit_new;
    }

    @Override
    public void initVariables() {
        Intent intent = getIntent();
        configBean = (ConfigBean) intent.getSerializableExtra("bean");
        position = intent.getIntExtra("position", 0);
        if (configBean.isEdit()) {
            isEdittext = true;
            setTitle("编辑配置");
        } else {
            isEdittext = false;
            setTitle("查看配置");
        }
//        setTitle("编辑模式");
        bleManageUtil = BleManageUtil.getInstance();

        String runType = spManager.getRunType();
        runTypelist = GsonUtil.jsonToList(runType, ConfigBean.class);

    }

    @Override
    public void initViews(Bundle savedInstanceState) {

        etInput1 = findViewById(R.id.et_input1);
        etInput2 = findViewById(R.id.et_input2);
        etInput3 = findViewById(R.id.et_input3);
        etInput4 = findViewById(R.id.et_input4);
        etInput6 = findViewById(R.id.et_input6);
        et2Input6 = findViewById(R.id.et2_input6);
        etInput7 = findViewById(R.id.et_input7);
        et2Input7 = findViewById(R.id.et2_input7);
        chCheck5 = findViewById(R.id.ch_check5);
        chCheck8 = findViewById(R.id.ch_check8);
        chCheck9 = findViewById(R.id.ch_check9);

        llLook = (LinearLayout) findViewById(R.id.ll_look);
        tvLookDetial = (TextView) findViewById(R.id.tv_look_detial);

        if (!isEdittext) {
            llLook.setVisibility(View.VISIBLE);
            etInput1.setEnabled(false);
            etInput2.setEnabled(false);
            etInput3.setEnabled(false);
            etInput4.setEnabled(false);
            etInput6.setEnabled(false);
            et2Input6.setEnabled(false);
            etInput7.setEnabled(false);
            et2Input7.setEnabled(false);
            chCheck5.setEnabled(false);
            chCheck8.setEnabled(false);
            chCheck9.setEnabled(false);
            tvLookDetial.setText(configBean.getValue());
        }else {
            llLook.setVisibility(View.GONE);
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void doBusiness() {
        if (isEdittext) {
            tvRight.setTextColor(this.getResources().getColor(R.color.white, null));
            tvRight.setVisibility(View.VISIBLE);
            setTvRight("保存配置");
            llRight.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ToastUtils.showCenter("保存配置");
//                    saveConfit();
                    sendConfigData();
                }
            });
            llLeft.setVisibility(View.GONE);
        }

        ProgressDlgUtil.showProgressDlg("读取中", this);
        MyBleGattCallback myBleGattCallback = new MyBleGattCallback() {
            @Override
            public void onConfigSuccess(ConfigDataFram dataFram) {
                ProgressDlgUtil.dismiss();
                if (dataFram == null) {
                    ToastUtils.showCenter("无法获取配置,请确认蓝牙是否连接");
                    finish();
                    return;
                } else {
                    configDataFram = bleManageUtil.getNotiflyConfigDataFrame();
                    LogUtil.d("获取配置 = " + GsonUtil.gsonString(configDataFram));
//                    initData(configDataFram);
                    setViewData();
                }
            }

            @Override
            public void onConfigFail() {
                super.onConfigFail();
                ProgressDlgUtil.dismiss();
                ToastUtils.showCenter("配置获取错误");
                finish();
            }
        };

        bleManageUtil.getConfigDataFrame(position, myBleGattCallback);
    }


    private void setViewData() {

        etInput1.setText((configDataFram.getcGroupName()));

        etInput2.setText((String.valueOf(configDataFram.getiTimeWork() / 10)));

        etInput3.setText((String.valueOf(configDataFram.getiMotorSpeed())));

        etInput4.setText(configDataFram.getcCountWork() + "");

        //工作位刀头浮动
        if (configDataFram.getcClrMode() == 1) {
            chCheck5.setChecked(true);
        } else {
            chCheck5.setChecked(false);
        }

        etInput6.setText(configDataFram.getiTimeSuper() / 10 + "");
        et2Input6.setText(configDataFram.getcCountSuper() + "");

        etInput7.setText(configDataFram.getiTimeWater() / 10 + "");
        et2Input7.setText(configDataFram.getcCountWater() + "");


        if (configDataFram.getcCountClear() == 1) {
            chCheck8.setChecked(true);
        } else {
            chCheck8.setChecked(false);
        }

        //7是有烘干  5 是无烘干
        LogUtil.d("转刀烘干configDataFram.getcClrSelect()  = " + configDataFram.getcClrSelect());
        if (7 == configDataFram.getcClrSelect()) {
            chCheck9.setChecked(true);
        } else {
            chCheck9.setChecked(false);
        }

    }

    private void sendConfigData() {

        try {
//            cClrMode  工作位刀头浮动
//        getInputConfig();
            //80 第一个用户组 81 第二个组
//            String hexPostion = Integer.toHexString(position);
//            String add = MathExtendUtils.add("80", hexPostion);
//            int parseInt3 = Integer.parseInt("80", 10);
            BigInteger bigint = new BigInteger("80", 16);
            int parseInt3 = bigint.intValue();
            int add1 = (int) MathExtendUtils.add(position, parseInt3);
//            LogUtil.d("第几个组 " + add);
            LogUtil.d("第几个组add1 " + add1);
            String s = Integer.toHexString(Integer.parseInt(String.valueOf(add1)));
            LogUtil.d("hex 转化 " + s);
            ConfigDataFram dataFram = new ConfigDataFram(s, "");
            //组名字
            String trim = etInput1.getText().toString().trim();
            configBean.setName(trim);
            runTypelist.set(position, configBean);
            spManager.saveRunType(GsonUtil.gsonString(runTypelist));

            dataFram.setcGroupName(trim);
            //4 均质工作时间  0-60
            int parseInt1 = Integer.parseInt(etInput2.getText().toString().trim());
            int temp = parseInt1 * 10;
            LogUtil.d("工作时间 " + temp);
            if (temp > 600) {
                ToastUtils.showCenter("研磨时间需要小于60s");
                return;
            }
            dataFram.setiTimeWork(temp);
            LogUtil.d(" 均质工作时间 = " + parseInt1);
            //均质工作下降时电机转速 转刀转速 上限18000
            int parseInt = Integer.parseInt(etInput3.getText().toString().trim());
            if (parseInt > 18000) {
                ToastUtils.showCenter("转速需要小于18000");
                return;
            }
            LogUtil.d(" 转速 = " + parseInt);
            dataFram.setiMotorSpeed(Integer.parseInt(etInput3.getText().toString().trim()));

            //渐进研磨
            int parseInt2 = Integer.parseInt(etInput4.getText().toString().trim());
            if (parseInt2 > 16) {
                ToastUtils.showCenter("渐进研磨需要小于16");
                return;
            }
            LogUtil.d(" 渐进研磨需 = " + parseInt2);
            dataFram.setcCountWork(parseInt2);

            //工作位刀头浮动
            if (chCheck5.isChecked()) {
                dataFram.setcClrMode(1);
            } else {
                dataFram.setcClrMode(0);
            }

            int parseInt4 = Integer.parseInt(etInput6.getText().toString().trim());
            LogUtil.d(" 超声波清洗时间 = " + parseInt4);
            //                //4 超声波清洗时间
            dataFram.setiTimeSuper(parseInt4 * 10);
//            //2 超声波位甩干次数 超声波清洗次数
            int parseInt5 = Integer.parseInt(et2Input6.getText().toString().trim());
            LogUtil.d(" 超声波清洗次数 = " + parseInt5);
            dataFram.setcCountSuper(parseInt5);

            dataFram.setiTimeWater(Integer.parseInt(etInput7.getText().toString().trim()) * 10);
            //清水清洗次数
            dataFram.setcCountWater((Integer.parseInt(et2Input7.getText().toString().trim())));

            if (chCheck8.isChecked()) {
                dataFram.setcCountClear(1);
            } else {
                dataFram.setcCountClear(0);
            }

            //7是有烘干  5 是无烘干
            if (chCheck9.isChecked()) {
                dataFram.setcClrSelect(7);
            } else {
                dataFram.setcClrSelect(5);
            }
//            dataFram.setiTimeWater(312);
//
//            dataFram.setcCountWater(1);
//            //是否添加洗涤剂
//            dataFram.setcCountClear(1);
//            //是否有烘干 转刀烘干
//            dataFram.setcClrSelect(5);
            LogUtil.d("写入配置 = " + GsonUtil.gsonString(configDataFram));
            LogUtil.d("dataFram写入配置 = " + GsonUtil.gsonString(dataFram));

            ProgressDlgUtil.showProgressDlg("保存中", this);
            MyBleGattCallback myBleGattCallback = new MyBleGattCallback() {
                @Override
                public void onConfigSuccess(ConfigDataFram dataFram) {
                    ProgressDlgUtil.dismiss();
                    if (dataFram == null) {
                        ToastUtils.showCenter("无法获取配置,请确认蓝牙是否连接");
                        finish();
                        return;
                    } else {
                        configDataFram = bleManageUtil.getNotiflyConfigDataFrame();
                        LogUtil.d("获取配置 = " + GsonUtil.gsonString(configDataFram));
//                    initData(configDataFram);
                        finish();
                    }
                }
            };
            bleManageUtil.writeConfigFrame(dataFram,myBleGattCallback);
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    /**
                     *要执行的操作
                     */
                    ProgressDlgUtil.dismiss();
                    ToastUtils.showCenter("数据传输完毕");
                    finish();
                }
            }, 3000);//3秒后执行Runnable中的run方法
        } catch (Exception e) {
            ProgressDlgUtil.dismiss();
            ToastUtils.showCenter("参数错误" + e.getMessage());
        }
    }

    @Override
    protected void onDestroy() {
        handler.removeCallbacksAndMessages(null);
        handler = null;
        super.onDestroy();
    }

}